<?php
declare (strict_types = 1);

namespace app\admin\controller;

use think\facade\Db;
use think\Request;
use app\admin\model\Node as NoModel;

class Role extends Base
{
    protected $middleware = [
        '\app\admin\middleware\LoginCheck',
        '\app\admin\middleware\UrlData',
        '\app\admin\middleware\UrlCheck'

    ];

    //角色控制器

    public function index() //角色列表
    {
        return view('');
    }

    public function data($limit,$page)  //角色表数据
    {
        $offset = ($page-1)*$limit;
        $limit = (int)$limit;

        $list = Db::table('role')
            ->alias('r')
            ->leftJoin('role_node r_n','r.id=r_n.role_id')
            ->leftJoin('node n','r_n.node_id=n.id')
            ->limit($offset,$limit)
            ->field('r.id,r.role_name,group_concat(n.node_name) node,r.create_time,r.update_time')
            ->group('r.id')
            ->select()->toArray();


        $count = Db::table('role')->count();

        foreach ($list as $k=>$v){
            if(empty($list[$k]['node'])){
                $list[$k]['node']='暂无权限';
           }
        }


        return json([
            "code"=>0,
            "msg"=>"",
            "count"=>$count,
            "data"=>$list
        ]);
    }

    public function update()    //角色的修改
    {
        if($this->request->isPost()){   //用户提交修改信息
            $data = $this->request->post();

            if(isset($data['node_id'])){
                $node_id = $data['node_id'];
                unset($data['node_id']);
            }


            $data['update_time'] = date("Y-m-d H:i:s");

            $res = Db::table('role')->update($data);
            $del = Db::table('role_node')->where('role_id',$data['id'])->delete();

            if($res){
                if(isset($node_id)){
                    $save = $this->makeNode($node_id,$data['id']);
                    $res = Db::table('role_node')->insertAll($save);
                    if($res){
                        $this->success('角色添加成功','index');
                    }
                }else{
                    $this->success('角色添加成功','index');
                }

                $res2 = Db::table('role_node')->insertAll($node_id);
                if($res2){
                    $this->success('角色修改成功','index');
                }

            }
            $this->error('角色修改失败');

        }


        $id = $this->request->get('id');    //获取修改用户id
        $role = Db::table('role')->where('id',$id)->field('id,role_name')->find();

        $role_node = Db::table('role_node')     //查询角色关联的节点
            ->where('role_id',$id)
            ->field('node_id')
            ->select()->toArray();

        $r_n = [];
        foreach ($role_node as $v){
            $r_n[] = $v['node_id'];
        }

        $node = $this->record();
        $node = $this->objToArray($node);



        return view('add',['role'=>$role,'list'=>$node,'r_n'=>$r_n]);
    }

    public function add()   //角色添加
    {
        if($this->request->isPost()){
            $data = $this->request->post();

            $data = $this->request->post();
            if(isset($data['node_id'])){
                $node_id = $data['node_id'];
                unset($data['node_id']);
            }


            $data = $this->insertTime($data);

            $user = Db::table('role')->where('role_name',$data['role_name'])->select()->toArray();

            if(!empty($user)){
                $this->error('已有相同的角色名');
            }


            $id = Db::table('role')->insertGetId($data);

            if($id){
                if(isset($node_id)){
                    $save = $this->makeNode($node_id,$id);
                    $res = Db::table('role_node')->insertAll($save);
                    if($res){
                        $this->success('角色添加成功','index');
                    }
                }else{
                    $this->success('角色添加成功','index');
                }

            }
            $this->error('角色添加失败');
        }

        $node = $this->record();
        $node = $this->objToArray($node);


        return view('',['list'=>$node]);
    }

    public function delete()    //角色删除
    {
        if($this->request->isPost()){
            $id = $this->request->post('id');

            $res = Db::table('role')->where('id',$id)->delete();
            $res2 = Db::table('role_node')->where('role_id',$id)->delete();

            if($res){
                return json([
                    'code'=>200,
                    'msg'=>'删除成功'
                ]);
            }
            return json([
                'code'=>400,
                'msg'=>'删除失败'
            ]);
        }
    }

    public function record($pid = 0){
        $list = NoModel::where("node_partent",$pid)->select();
        if(!empty($list)){
            foreach ($list as $k=>$v){
                $children = $this->record($v['id']);
                if(!empty($children)){
                    $list[$k]["children"] = $children;
                }
            }
        }
        return $list;
    }

    private function makeNode($node,$id)
    {
        $node_id = [];


        foreach ($node as $v){   //对 // 这种节点id进行处理
            $node_id = array_merge($node_id,explode('/',$v));
        }
        $node_id = array_unique($node_id);  //给节点元素去重

        $save = [];
        foreach ($node_id as $v){
            $save[] = ['role_id'=>$id,'node_id'=>$v,'create_time'=>date("Y-m-d H:i:s"),'update_time'=>date("Y-m-d H:i:s")];
        }

        return $save;
    }

}
